package com.hgkj.cloud.base.service;

import com.hgkj.cloud.base.domain.AdminUser;
import com.hgkj.cloud.base.domain.AdminUserChannel;
import com.hgkj.cloud.base.domain.EUser;
import com.hgkj.cloud.base.domain.VO.ExpertUsersInfo;
import com.hgkj.cloud.base.util.HcStringUtils;
import com.hgkj.cloud.base.dao.AdminUserChannelMapper;
import com.hgkj.cloud.base.dao.AdminUserMapper;
import com.hgkj.cloud.base.dao.EUserMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private EUserMapper eUserMapper;
    @Autowired
    private AdminUserChannelMapper adminUserChannelMapper;

    public int insertAdminUser(AdminUser adminUser) {
        return adminUserMapper.insertAdminUser(adminUser);
    }

    public int updateAdminUser(AdminUser adminUser) {
        return adminUserMapper.updateAdminUser(adminUser);
    }

    public int delAdminUserById(String uuid) {
        return adminUserMapper.delAdminUserById(uuid);
    }

    public int delAdminUserByFrameworkId(Map<String, Object> map) {
        return adminUserMapper.delAdminUserByFrameworkId(map);
    }

    public int PhysicsDelAdminUserById(String uuid) {
        return adminUserMapper.PhysicsDelAdminUserById(uuid);
    }

    public AdminUser getAdminUserByMap(Map<String, Object> map) {
        return adminUserMapper.getAdminUserByMap(map);
    }

    public List<AdminUser> listAdminUsers(Map<String, Object> map) {
        checkAndSetOwnerChannel(map);
        return adminUserMapper.listAdminUsers(map);
    }

    private void checkAndSetOwnerChannel(Map<String, Object> map) {
        String channel_owner_uuid = HcStringUtils.parseString(map.get("channel_owner_uuid"), null);
        List<String> ownerChannels = new ArrayList<>();
        if (channel_owner_uuid != null) {
            Map<String, Object> aamap = new HashMap<>();
            aamap.put("uuid", channel_owner_uuid);
            AdminUser adminUser = getAdminUserByMap(aamap);
            if (adminUser != null && StringUtils.isNotEmpty(adminUser.getChannel_id())) {
                ownerChannels.add(adminUser.getChannel_id());
            } else {
                EUser eUser = eUserMapper.getUserInfo(aamap);
                if (eUser != null && StringUtils.isNotEmpty(eUser.getChannel_id())) {
                    ownerChannels.add(eUser.getChannel_id());
                }
            }
            List<AdminUserChannel> adminUserChannels = adminUserChannelMapper.listByUuid(channel_owner_uuid);
            if (adminUserChannels != null) {
                for(AdminUserChannel adminUserChannel : adminUserChannels) {
                    ownerChannels.add(adminUserChannel.getChannelId());
                }
            }
            map.put("ownerChannels", ownerChannels);
        }
    }

    public List<AdminUser> listChannelAdminUsers(Map<String, Object> map) {
        return adminUserMapper.listChannelAdminUsers(map);
    }

    public Long listAdminUsersCount(Map<String, Object> map) {
        checkAndSetOwnerChannel(map);
        return adminUserMapper.listAdminUsersCount(map);
    }

    public List<ExpertUsersInfo> listAllAdminUsers(Map<String, Object> map) {
        return adminUserMapper.listAllAdminUsers(map);
    }

    public int listAllAdminUsersCount(Map<String, Object> map) {
        return adminUserMapper.listAllAdminUsersCount(map);
    }

    public List<AdminUser> listAdminUsersWhitFa(Map<String, Object> map) {
        return adminUserMapper.listAdminUsersWhitFa(map);
    }

    public Long listAdminUsersWhitFaCount(Map<String, Object> map) {
        return adminUserMapper.listAdminUsersWhitFaCount(map);
    }

    public AdminUser getAdminUserDataByMap(Map<String, Object> map) {
        return adminUserMapper.getAdminUserDataByMap(map);
    }

    public List<AdminUser> listAllAdminUsersByMap(Map<String, Object> map) {
        return adminUserMapper.listAllAdminUsersByMap(map);
    }

    public Map<String, Object> getPsyInfoByUuid(String admin_uuid) {
        return adminUserMapper.getPsyInfoByUuid(admin_uuid);
    }

    public int getSuperUser(Map<String, Object> map) {
        return adminUserMapper.getSuperUser(map);
    }

    public Map<String, Object> getPsyAllInfoByUuid(String adminUuid) {
        return adminUserMapper.getPsyAllInfoByUuid(adminUuid);
    }

}